package com.five.dao.impl.backstage;

import com.five.dao.backstage.RoomQueDao;
import com.five.entity.backstage.TRoomQuery;
import com.five.util.DBUtil;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: jianglin
 * Date: 2020/11/23  15:48
 */
@Repository
public class RoomQueDaoImpl  implements RoomQueDao {
    @Override
    public List<Map> queryAll(Integer page, Integer limit, TRoomQuery tRoomQuery) {
        String sql="select r.id ,r.roomcode,t.name,r.status from t_room r,t_type t where r.typeid=t.id";
        if (tRoomQuery.getRoomcode()!=null){
            sql+=" and r.room like '%"+tRoomQuery.getRoomcode()+"%' ";
        }
        if (tRoomQuery.getName()!=null){
            sql+=" and t.name like '%"+tRoomQuery.getName()+"%' ";
        }
        sql+=" limit ?,?";

        return DBUtil.queryMap(sql,(page-1)*limit,limit);
    }

    @Override
    public int deleteId(int id) {
        String sql="delete from t_room where id=?";
        return DBUtil.manage(sql,id);
    }

    @Override
    public int addRoom(TRoomQuery tRoomQuery) {
        String sql="insert into t_room(roomcode,typeid) values(?,?)";
        return DBUtil.manage(sql,tRoomQuery.getRoomcode(),tRoomQuery.getTypeid());
    }

    @Override
    public List<Map> queryRoomType() {
        String sql="select id,name from t_type where status=0";
        return  DBUtil.queryMap(sql);
    }

    @Override
    public List<Map> selectRoomNumber(String roomcode) {
        String sql="select * from t_room where roomcode=?";
        return DBUtil.queryMap(sql,roomcode);
    }

    @Override
    //修改 房间类型 房间号 房间状态
    public int updata(TRoomQuery tRoomQuery) {
        String sql = "update t_room set roomcode=?,typeid=? where id=?";
        return DBUtil.manage(sql,tRoomQuery.getRoomcode(),tRoomQuery.getTypeid(),tRoomQuery.getId());
    }

    @Override
    public int getAllCount(TRoomQuery tRoomQuery) {
        String sql="select count(*) a from t_room r,t_type t where r.typeid=t.id";
        if (tRoomQuery.getRoomcode()!=null){
            sql+=" and r.roomcode like '%"+tRoomQuery.getRoomcode()+"%' ";
        }
        if (tRoomQuery.getName()!=null){
            sql+=" and t.name like '%"+tRoomQuery.getName()+"%' ";
        }
        List<Map> maps = DBUtil.queryMap(sql);
        Map map = maps.get(0);
        Object a = maps.get(0).get("a");
        int i = Integer.parseInt(a+"");
        return i;
    }


    @Override
    public int bad(Integer id) {
        String sql="update t_room set status=1 where id=?";
        int i = DBUtil.manage(sql, id);
        return i;
    }

    @Override
    public int good(Integer id) {
        String sql="update t_room set status=0 where id=?";
        int i = DBUtil.manage(sql, id);
        return i;
    }
}
